package com.yzy.controller;

import com.yzy.entity.Books;
import com.yzy.entity.Doctors;
import com.yzy.entity.Patients;
import com.yzy.utils.Utils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorBookFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out;
        response.setContentType("text/html;charset=utf-8");
        out=response.getWriter();
        ServletContext application = request.getServletContext();
        String DlongindidString= (String) application.getAttribute("Dlongindid");
        Integer Dlongindid=Integer.valueOf(DlongindidString);

//找医生的姓名
        String sql2 ="select dname from doctor where did=?";
        Utils util2 = new Utils();
        PreparedStatement ps2=util2.createStatement(sql2);
        ResultSet rs2=null;
        List<Doctors> booklist2=new ArrayList<>();
        try {
            ps2.setInt(1,Dlongindid);
            rs2 =ps2.executeQuery();
            while (rs2.next()){
                String dname=rs2.getString("dname");
                Doctors doctors=new Doctors(dname);
                booklist2.add(doctors);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            util2.close(rs2);
            util2.close();
        }

        //找到预约表的5个字段
        Utils util = new Utils();
        String sql = "select * from book where did=?";
        PreparedStatement ps =util.createStatement(sql);
        List<Books> booklist=new ArrayList<>();
        ResultSet rs =null;
        try {
            ps.setInt(1,Dlongindid);
            rs = ps.executeQuery();
            while (rs.next()){

                String did=rs.getString("did");
                String pphone=rs.getString("pphone");
                String time=rs.getString("time");
                String booktime=rs.getString("booktime");
                String state=rs.getString("state");
                Books books = new Books(pphone,did,time,state,booktime);

                booklist.add(books);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }

        out.print("<table boder='2' align='center'>");
        out.print("<tr>");
        out.print("<td>医生编号</td>");
        out.print("<td>医生姓名</td>");
        out.print("<td>患者手机号</td>");
        out.print("<td>操作时间</td>");
        out.print("<td>就诊时间</td>");
        out.print("<td>就诊情况</td>");

            out.print("<td>就诊</td>");

        out.print("</tr>");
        for(Books book : booklist){
            out.print("<tr>");
            out.print("<td>"+book.getDid()+"</td>");
            out.print("<td>"+booklist2.get(0).getDname()+"</td>");
            out.print("<td>"+book.getPphone()+"</td>");
            out.print("<td>"+book.getTime()+"</td>");
            out.print("<td>"+book.getBooktime()+"</td>");
            out.print("<td>"+book.getState()+"</td>");

//            if(booklist.get(0).getState()!="finish") {
                out.print("<td><a href='/myWeb/doctor/jiuzhen?pphone="+book.getPphone()+"'>点击就诊</a></td>");
//            }
                      out.print("</tr>");
        }
        out.print("</table>");
    }
}
